/// 
/// MaDAson - Toolkit for Model Driven Software Development
/// 
/// Copyright [2006] Marcus Lankenau
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///  
///     http://www.apache.org/licenses/LICENSE-2.0
///  
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
/// 

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Madason.GenUtils
{
	public class ProtectedSections
	{
		Dictionary<string, string> sections = new Dictionary<string, string>();

		/// <summary>
		/// set content for a section (used for unit-tests)
		/// </summary>
		/// <param name="id"></param>
		/// <param name="content"></param>
		public void SetSectionContent(string id, string content)
		{
			sections[id] = content;
		}


		/// <summary>
		/// load a file and extract all section
		/// </summary>
		/// <param name="fileInfo"></param>
		public void Load(FileInfo fileInfo)
		{
			if (fileInfo.Exists)
			{
				using (StreamReader streamReader = fileInfo.OpenText())
				{
					string fileContent = streamReader.ReadToEnd();
					streamReader.Close();

					Load(fileContent, fileInfo.FullName);
				}
			}
		}


		/// <summary>
		/// load a file and extract all sections
		/// </summary>
		/// <param name="buffer"></param>
		/// <param name="filepath"></param>
		public void Load(string buffer, string filepath)
		{
			Regex regexBegin = new Regex(@"\{\{\{\{\{\[([a-zA-Z_]+)\]");
			                                 
			Regex regexEnd = new Regex(@"\}\}\}\}\}");

			while (true)
			{

				Match match = regexBegin.Match(buffer);
				if (!match.Success) break;

				string id = match.Groups[1].Value;
				int firstChar = match.Index + match.Length;

				Match matchEnd = regexEnd.Match(buffer, firstChar);
				if (!matchEnd.Success)
					throw new Exception("can't find end of protected section " + id + " in file " + filepath);

				int lastChar = matchEnd.Index - 1;

				string value = buffer.Substring(firstChar, lastChar-firstChar+1);

				sections.Add(id, value);

				buffer = buffer.Substring(matchEnd.Index + matchEnd.Length);
			}
		}


		/// <summary>
		/// generate a section for xml
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public string GenForXml(string id)
		{			
			string value = null;
			if (sections.ContainsKey(id))
				value = sections[id];
			else
				value = " INSERT YOUR OWN CODE HERE ";
			return "<!-- {{{{{[" + id + "]"+value+"}}}}} -->";						
		}


		/// <summary>
		/// generate a section for xml
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public string GenForJava(string id, int numTabs)
		{
			string tabs = "";
			for (int i = 0; i < numTabs; i++)
				tabs += "\t";

			string value = null;
			if (sections.ContainsKey(id))
				value = sections[id];
			else
				value = " */\r\n"+tabs+"// INSERT YOUR OWN CODE HERE \r\n"+tabs+"/*";
			return tabs + "/* {{{{{[" + id + "]" + value + "}}}}} */";
		}	
	
	}
}
